<?php
class BaseController extends Controller {

    protected $JS_PATH = '';
    protected $CSS_PATH = '';
    protected $title = '';  //页面标题
    public $layout = '/layouts/common';  //布局文件
    public function init() {
        $this->JS_PATH = Yii::app()->baseUrl . '/static/' . $this->module->getId() . '/js/';
        $this->CSS_PATH = Yii::app()->baseUrl . '/static/' . $this->module->getId() . '/css/';
    }

    protected function error($err_msg) {
        if (Yii::app()->request->isAjaxRequest) {
            $this->jsonResponse(array('code'=>1000,'message'=>$err_msg));
        } else {
            $this->renderPartial('../common/error', array('error_msg' => $err_msg));
            exit;
        }
    }

    /**
     * 操作成功提示
    */
    protected function success($message, $data = array()) {
        $rep_data = array(
            'code' => 0,
            'message' => $message,
            'data' => $data
        );
        $this->jsonResponse($rep_data);
    }

    /**
     * 返回json数据
    */
    protected function jsonResponse($data) {
        header('Content-Type:application/json; charset=utf-8');
        if (empty($data) || !is_array($data)) {
            $json = CJSON::encode(array());
        } else {
            $json = CJSON::encode($data);
        }
        exit($json);
    }
}